package com.yswl.yeeduo.ui.activity;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import android.os.Bundle;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.http.KAsyncTask.TaskDelegate;
import com.yswl.yeeduo.tool.KStringTool;
import com.yswl.yeeduo.ui.adapter.DeitalMoneyAdapter;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.bean.DetailMoneyBean;
import com.yswl.yeeduo.ui.bean.Page;
import com.yswl.yeeduo.ui.task.DeitalMoneyTask;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.view.CommonEmptyView;
import com.yswl.yeeduo.view.listview.xlistview.XListView;
import com.yswl.yeeduo.view.listview.xlistview.XListView.IXListViewListener;

/**
 * 收益明细
 * 
 * @author Administrator
 * 
 */
public class DetailMoneyActivity extends BaseActivity implements TaskDelegate, IXListViewListener {
	private DeitalMoneyAdapter mAdapter;
	ArrayList<DetailMoneyBean> mDatas;
	private Page mPage = new Page();
	private XListView xListView;

	private final static int REFLUSH = 20200;
	private final static int LOADMORE = 20222;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_deitalmoney);
		super.setHeader("收益明细");

		initUI();
		getData();
	}

	private void initUI() {
		xListView = (XListView) findViewById(R.id.user_deital_list);
		xListView.setPullLoadEnable(true);
		xListView.setPullRefreshEnable(true);
		xListView.setXListViewListener(this);
		mPage.setCurPage(1);
		mAdapter = new DeitalMoneyAdapter(this);
		xListView.setAdapter(mAdapter);
		CommonEmptyView.setCommonEmptyView(this, xListView, null);
	}

	private void getData() {

		if (mDatas == null) {
			// 异步任务 请求数据
			sendDeitalMoneyRequest(REFLUSH);
		} else {
			mAdapter.setData(mDatas);
		}

	}

	private void sendDeitalMoneyRequest(int requestId) {
		Map<String, String> params = new TreeMap<String, String>();
		String userId = ServiceAuthManager.getUserId(this);
		if (KStringTool.isEmpty(userId)) {
			userId = "-1";
		}
		params.put("userId", userId);
		params.put("api_key", ServiceAuthManager.getToken(this));
		params.put("offset", mPage.getStart() + "");
		params.put("count", mPage.getPageSize() + "");
		new DeitalMoneyTask(this, requestId, this).execute(params);
	}

	// Um
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName());
		MobclickAgent.onResume(this); // 统计时长
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
		MobclickAgent.onPause(this);
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		xListView.stopLoadMore();
		xListView.stopRefresh();
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		mDatas = (ArrayList<DetailMoneyBean>) object;
		switch (taskId) {
		case REFLUSH:
			mAdapter.setData(mDatas);
			break;
		case LOADMORE:
			if (mDatas != null) {
				mAdapter.addData(mDatas);
			}
			break;

		default:
			break;
		}
		xListView.stopLoadMore();
		xListView.stopRefresh();

	}

	@Override
	public void onRefresh() {
		mPage.setCurPage(1);
		sendDeitalMoneyRequest(REFLUSH);
	}

	@Override
	public void onLoadMore() {
		mPage.setCurPage(mPage.getCurPage()+1);
		sendDeitalMoneyRequest(LOADMORE);
	}

}
